package com.tianyang.yukuaifuzs.business.main.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.main.contract.ServiceRegisterContract;
import com.tianyang.yukuaifuzs.business.main.presenter.ServiceRegisterPresenter;
import com.tianyang.yukuaifuzs.utils.SoftInputUtil;
import com.tianyang.yukuaifuzs.utils.ToastUtil;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：服务商信息
 * Created by xyj on 2024/10/20
 */
public class ServiceRegisterActivity extends BaseTitleActivity implements ServiceRegisterContract.View {

    @BindView(R.id.agentNameEt)
    EditText agentNameEt;
    @BindView(R.id.phoneEt)
    EditText phoneEt;
    @BindView(R.id.passwordEt)
    EditText passwordEt;

    private ServiceRegisterContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_service_register;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("服务商信息");

        mPresenter = new ServiceRegisterPresenter(this);
    }

    @OnClick(R.id.tv_add)
    public void onClick(View view) {
        if (view.getId() == R.id.tv_add) {
            if (checkParameter()) {
                mPresenter.agentRegisterNew(agentNameEt.getText().toString(),
                        phoneEt.getText().toString(), passwordEt.getText().toString());
            }
        }
    }

    /**
     * 校验输入框
     */
    private boolean checkParameter() {
        String userName = String.valueOf(agentNameEt.getText());
        String inviteAgentPhoneNo = String.valueOf(phoneEt.getText());
        String password = String.valueOf(passwordEt.getText());
        if (TextUtils.isEmpty(userName)) {
            ToastUtil.showTextViewPromptShort("请输入代理商名称");
            SoftInputUtil.openKeybord(agentNameEt, mContext);
            return false;
        }
        if (TextUtils.isEmpty(inviteAgentPhoneNo)) {
            ToastUtil.showTextViewPromptShort("请输入登录手机号");
            SoftInputUtil.openKeybord(phoneEt, mContext);
            return false;
        } else if (VerifyUtil.checkPhoneNumber(inviteAgentPhoneNo)) {
            ToastUtil.showTextViewPromptShort("登录手机号输入不正确");
            SoftInputUtil.openKeybord(phoneEt, mContext);
            return false;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.showTextViewPromptShort("密码不能为空");
            SoftInputUtil.openKeybord(passwordEt, mContext);
            return false;
        }
        if (password.length() < 6) {
            ToastUtil.showTextViewPrompt("密码不能小于6位数");
            SoftInputUtil.openKeybord(passwordEt, mContext);
            return false;
        }
        return true;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(ServiceRegisterContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
